package com.service.store.impl;

import com.dao.store.QuestionItemDao;
import com.domain.store.QuestionItem;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.store.QuestionItemService;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(String qusetid,int page, int size) {
        PageInfo pageInfo = null;
        try {
            SqlSession session= MapperUtils.init();
            QuestionItemDao mapper = session.getMapper(QuestionItemDao.class);
            Page pages = PageHelper.startPage(page,size);
            mapper.findAll(qusetid);
            pageInfo = new PageInfo(pages);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public QuestionItem findById(String id) {
        QuestionItem questionItem = null;
        try {
            SqlSession session= MapperUtils.init();
            QuestionItemDao mapper = session.getMapper(QuestionItemDao.class);
            questionItem = mapper.findById(id);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return questionItem;
    }

    /**
     * 添加操作
     * @param questionItem
     */
    @Override
    public String save(QuestionItem questionItem,boolean flag) {
        String id = null;
        try {
            SqlSession session= MapperUtils.init();
            QuestionItemDao mapper = session.getMapper(QuestionItemDao.class);
            id = UUID.randomUUID().toString();
            questionItem.setId(id);
            if (flag) {
                //防止图片重名
                questionItem.setPicture(id);
            }
            mapper.save(questionItem);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return id;
    }

    /**删除操作
     * @param questionItem
     */
    @Override
    public void delete(QuestionItem questionItem) {
        try {
            SqlSession session= MapperUtils.init();
            QuestionItemDao mapper = session.getMapper(QuestionItemDao.class);
            mapper.delete(questionItem);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改操作
     * @param questionItem
     */
    @Override
    public void update(QuestionItem questionItem,boolean flag) {
        try {
            SqlSession session= MapperUtils.init();
            QuestionItemDao mapper = session.getMapper(QuestionItemDao.class);
            if (flag){
                questionItem.setPicture(questionItem.getId());
            }
            mapper.update(questionItem);
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
